package com.fieldchain.ex.fa100.ui.trade.fragment;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.access.Constant;
import com.fieldchain.ex.fa100.bean.EntiustBean;
import com.fieldchain.ex.fa100.ui.trade.adapter.TradEntiustAdapter;
import com.fieldchain.ex.fa100.utils.screen.MyToast;
import com.fieldchain.ex.fa100.utils.share.ShareManager;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenu;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItem;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.fieldchain.ex.fa100.utils.net.access.HttpRequest;
import com.fieldchain.ex.fa100.utils.net.modle.ListRequestTwo;
import com.fieldchain.ex.fa100.utils.net.modle.StringRequest;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/20
 */
public class TradEntiustFragment extends BaseTradFragment {
    private RadioGroup rg;
    private RadioButton rb0;
    private RadioButton rb1;
    private RadioButton rb2;
    private SmartRefreshLayout refresh;
    private RelativeLayout reaEmpty;
    private TextView tvEmpty;
    private SwipeMenuRecyclerView rv;

    private SwipeMenuCreator swipeMenuCreator;
    private int index=0;
    private int page = 1;
    private List<EntiustBean> mList = new ArrayList<>();
    private TradEntiustAdapter adapter;
    @Override
    protected void findView() {
        rg = view.findViewById(R.id.rg);
        rb0 = view.findViewById(R.id.rb0);
        rb1 = view.findViewById(R.id.rb1);
        rb2 = view.findViewById(R.id.rb2);
        refresh = view.findViewById(R.id.refresh);
        reaEmpty = view.findViewById(R.id.rea_empty);
        tvEmpty = view.findViewById(R.id.tv_empty);
        rv = view.findViewById(R.id.rv);
    }

    @Override
    protected void setValue() {
        setCreater();
        initRefresh();
        initRv();

        setTags(rb0,0);
        setTags(rb1,1);
        setTags(rb2,2);
    }


    private void initRv() {
        adapter=new TradEntiustAdapter(mContext);
        rv.setLayoutManager(new LinearLayoutManager(mContext));
        rv.setAdapter(adapter);
    }

    @Override
    public int getLayout() {
        return R.layout.fragment_trad_entiust;
    }

    @Override
    public void setCompany() {

    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e("onResum88888","onResume");
        mList.clear();
        getData();
    }


    public void setCreater() {
        String empty="          ";
        final String tab=empty+"撤单"+empty;
        swipeMenuCreator = new SwipeMenuCreator() {
            @Override
            public void onCreateMenu(SwipeMenu swipeLeftMenu, SwipeMenu swipeRightMenu, int viewType) {
                SwipeMenuItem deleteItem = new SwipeMenuItem(mContext)
                        .setBackground(R.drawable.shape_button_red)
                        .setText(tab) // 文字。
                        .setTextColor(Color.WHITE) // 文字颜色。
                        .setHeight(ViewGroup.LayoutParams.MATCH_PARENT)
                        .setTextSize(16);// 文字大小。
                swipeRightMenu.addMenuItem(deleteItem);// 添加一个按钮到右侧侧菜单。.
            }
        };

        SwipeMenuItemClickListener mMenuItemClickListener = new SwipeMenuItemClickListener() {
            @Override
            public void onItemClick(SwipeMenuBridge menuBridge) {
                menuBridge.closeMenu();
                int adapterPosition = menuBridge.getAdapterPosition(); // RecyclerView的Item的position。

                EntiustBean bean=mList.get(adapterPosition);
                HashMap map=new HashMap();
                map.put("id",bean.getId());
                map.put("market",bean.getMarket());
                map.put("dealType",bean.getDealType());
                map.put("status","4");
                map.put("price",bean.getPrice());
                remove(new Gson().toJson(map));
                mList.remove(adapterPosition);
                adapter.setData(mList);
                if (mList.size() > 0) {
                    reaEmpty.setVisibility(View.GONE);
                } else {
                    reaEmpty.setVisibility(View.VISIBLE);
                }
            }
        };

        rv.setSwipeMenuCreator(swipeMenuCreator);
        rv.setSwipeMenuItemClickListener(mMenuItemClickListener);
    }

    public void initRefresh() {
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                refreshLayout.finishLoadMore(1000);
                getData();
            }
        });

        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                mList.clear();
                refresh.finishRefresh(1000);
                getData();
            }
        });
    }

    public void getData() {
        Log.e("onResum88888","getData");
        HashMap map = new HashMap();
        map.put("page", page + "");
        map.put("market",market.toLowerCase());
        map.put("status",1+"");
        if(index!=0){
            map.put("type", index + "");
        }
        HttpRequest.getInstance().getRequest(map,Constant.entiust, new ListRequestTwo<EntiustBean>() {
            @Override
            public void onSucess(List<EntiustBean> list, int code, String msg) {
                Log.e("onResum88888",list.size()+"onSucess"+code);
                if (code == 200) {
                    mList.addAll(list);
                    adapter.setData(mList);
                }
                if (list.size() == 0) {
                    if (page > 1) {
                        page--;
                    }
                }
                if (mList.size() > 0) {
                    reaEmpty.setVisibility(View.GONE);
                } else {
                    reaEmpty.setVisibility(View.VISIBLE);
                }
            }
            @Override
            public void onFaild(Exception e) {
                if (page > 1) {
                    page--;
                }
                if (mList.size() > 0) {
                    reaEmpty.setVisibility(View.GONE);
                } else {
                    reaEmpty.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    public void remove(String json){
        HttpRequest.getInstance().postRequest(json, Constant.removeTrad,new StringRequest() {
            @Override
            public void onSucess(String str, int code, String msg) {

            }

            @Override
            public void onFaild(Exception e) {

            }
        });
    }

    public void setTags(RadioButton bt, final int tag) {
        bt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    page = 1;
                    mList.clear();
                    refresh.finishRefresh(1000);
                    index=tag;
                    getData();
                }
            }
        });
    }
    @Override
    public void loginShow() {
        if(ShareManager.getInstance().getUser()==null) {
            MyToast.show("未登录");
        }else{
            MyToast.show("登录过期");
        }
    }
}
